package com.itheima.stock.controller;

import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.UserService;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.req.UserReqVo;
import com.itheima.stock.vo.resp.LoginRespVo;
import com.itheima.stock.vo.resp.PageResult;
import com.itheima.stock.vo.resp.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author by itheima
 * @Date 2021/12/29
 * @Description 定义用户访问层
 */
@RestController
@RequestMapping("/api")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录功能实现
     *
     * @param vo
     * @return
     */
/*    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo){
        R<LoginRespVo> r= userService.login(vo);
        return r;
    }*/

    /**
     * 生成验证码
     *  map结构：
     *      code： xxx,
     *      rkey: xxx
     * @return
     */
    @GetMapping("/captcha")
    public R<Map> generateCaptcha(){
        return this.userService.generateCaptcha();
    }

    /**
     * 添加用户信息
     * @param sysUser
     * @return
     */
    @PostMapping("/user")
    public R<String> insertUserInfo(@RequestBody SysUser sysUser) {
        return userService.insertUserInfo(sysUser);
    }

    /**
     * 根据用户id查询用户所分配的角色 查询所有的角色
     * @param userId
     * @return
     */
    @GetMapping("/user/roles/{userId}")
    public R<Map<String, Object>> queryUserRoleInfo(@PathVariable("userId") String userId) {
        return userService.queryUserRoleInfo(userId);
    }

    /**
     * 多条件综合查询用户分页信息，条件包含：分页信息 用户创建日期范围
     *
     * @param vo
     * @return
     */
    @PostMapping("/users")
//    @PreAuthorize("hasAuthority('sys:user:list')")
    public R<PageResult<SysUser>> queryUserInfo(@RequestBody UserReqVo vo) {
        return userService.queryUserInfo(vo);
    }

    /**
     * 修改页面回显信息
     * @param userId
     * @return
     */
    @GetMapping("/user/info/{userId}")
    public R<SysUser> getById(@PathVariable("userId") Long userId) {
        return userService.getById(userId);
    }

    /**
     * 修改用户信息
     * @param sysUser
     * @return
     */
    @PutMapping("/user")
    public R<String> updateUserInfo(@RequestBody SysUser sysUser) {
        return userService.update(sysUser);
    }

    /**
     * 批量删除用户信息
     * @param ids
     * @return
     */
    @DeleteMapping("/user")
    public R<String> delUserInfo(@RequestBody List<Long> ids) {
        return userService.delete(ids);
    }

    /**
     * 获取
     * @return userService
     */
    public UserService getUserService() {
        return userService;
    }

    /**
     * 设置
     * @param userService
     */
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String toString() {
        return "UserController{userService = " + userService + "}";
    }
}